﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using System.IO;
using AttributeRouting;
using AttributeRouting.Web.Mvc;
using PH.Model.Schools;
using PH.Logic.Schools;
using PH.Model.Results;
using Newtonsoft.Json.Linq;
using PH.Model.Commons;
using PH.Logic.Commons;

namespace PH.Web.Management.Areas.School.Controllers
{
    [RouteArea("School")]
    [RoutePrefix("Student")]
    public class StudentApiController : ApiController
    {
        [Route("GetStudents")]
        [HttpPost]
        public JsonEntitySet<StudentModel> GetStudents(JObject request)
        {
            string errinfo = "no data";
            try
            {
                StudentQuery search = new StudentQuery();
                search.PageIndex = request.Value<int>("pageIndex");
                search.PageSize = request.Value<int>("pageSize");
                var schGuid = request.Value<string>("schoolGuid");
                if (!string.IsNullOrEmpty(schGuid))
                    search.SchoolGuid = new Guid(schGuid);
                var claGuid = request.Value<string>("classGuid");
                if (!string.IsNullOrEmpty(claGuid))
                    search.ClassGuid = new Guid(claGuid);
                search.GradeCode = request.Value<int>("gradeIndex");
                search.Key = request.Value<string>("key");
                var result = StudentLogic.GetStudents(search);
                return new JsonEntitySet<StudentModel>(result);
            }
            catch (Utils.PhException pex) { errinfo = pex.Message; }
            catch (Exception ex)
            {
                errinfo = ex.Message;
                Common.Logger.Error("", ex);
            }
            return new JsonEntitySet<StudentModel>(null, errinfo, false);
        }
        [Route("SaveStudent")]
        [HttpPost]
        public JsonEntity<StudentModel> SaveStudent(JObject request)
        {
            string errinfo = "对不起！保存失败，出错原因：";
            try
            {
                #region 收集参数
                StudentModel stu = new StudentModel();
                var guid = request.Value<string>("guid");
                stu.Guid = new Guid(guid);
                stu.ID = request.Value<int>("stuID");
                stu.ClassID = request.Value<int>("classID");
                stu.StudentNo = request.Value<string>("studentNo");
                stu.RealName = request.Value<string>("realName");
                stu.Sex = request.Value<byte>("sex");
                stu.Mobile = request.Value<string>("mobile");
                stu.NationID = 1;
                stu.OriSchoolCode = request.Value<string>("oriSchoolCode");
                #endregion

                if (stu.Guid == Guid.Empty)
                    stu = StudentLogic.CreateNewStudent(stu);
                else
                    StudentLogic.UpdateStudent(stu);
                return new JsonEntity<StudentModel>(stu);
            }
            catch (Utils.PhException pex) { errinfo = pex.Message; }
            catch (Exception ex)
            {
                errinfo = ex.Message;
                Common.Logger.Error("", ex);
            }
            return new JsonEntity<StudentModel>(null, errinfo, false);
        }
        [Route("DeleteStudent")]
        [HttpPost]
        public JsonResult<bool> DeleteStudent(JObject request)
        {
            string errinfo = "对不起！删除失败，出错原因：";
            try
            {
                #region 收集参数
                var guid = request.Value<string>("guid");
                var stuGuid = Guid.Empty;
                if (!string.IsNullOrEmpty(guid))
                    stuGuid = new Guid(guid);

                #endregion
                StudentLogic.DeleteStudent(stuGuid);
                return new JsonResult<bool>(true);
            }
            catch (Utils.PhException pex) { errinfo = pex.Message; }
            catch (Exception ex)
            {
                errinfo = ex.Message;
                Common.Logger.Error("", ex);
            }
            return new JsonResult<bool>(false, errinfo, false);
        }
        [Route("DelStudents")]
        [HttpPost]
        public JsonResult<int> DelStudents(JObject request)
        {
            string errinfo = "对不起！删除失败，出错原因：";
            try
            {
                #region 收集参数
                var guids = request.Value<string>("guids");
                var guidArray = guids.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
                var lstGuid = new List<Guid>();
                foreach (var str in guidArray)
                    lstGuid.Add(new Guid(str));
                #endregion
                var ret=StudentLogic.DeleteStudents(lstGuid);
                return new JsonResult<int>(ret);
            }
            catch (Utils.PhException pex) { errinfo = pex.Message; }
            catch (Exception ex)
            {
                errinfo = ex.Message;
                Common.Logger.Error("", ex);
            }
            return new JsonResult<int>(0, errinfo, false);
        }
        [Route("ImportStudent")]
        [HttpPost]
        public JsonResult<bool> ImportStudent(JObject request)
        {
            string errinfo = "对不起！保存失败，出错原因：";
            try
            {
                var fGuid = request.Value<string>("fGuid");
                var sGuid = request.Value<string>("sGuid");
                if (string.IsNullOrEmpty(fGuid))
                    throw new Utils.PhException("没有找到上传的文件");
                if (string.IsNullOrEmpty(sGuid))
                    throw new Utils.PhException("无效的学校编号");
                var eGuid = StudentLogic.ImportStudent(new Guid(fGuid), new Guid(sGuid));
                if (eGuid == Guid.Empty)
                    return new JsonResult<bool>(true);

                errinfo = string.Format("导入学生数据失败，请下载结果文件查看详情。<div style='text-align:center; padding:10px'><a href='/Files/DownLoad/{0}' target='_blank'>详情请点击下载Excel文件</a></div>", eGuid);
            }
            catch (Utils.PhException pex) { errinfo = pex.Message; }
            catch (Exception ex)
            {
                errinfo = ex.Message;
                Common.Logger.Error("", ex);
            }
            return new JsonResult<bool>(false, errinfo, false);
        }
        [Route("ChangeIDNO")]
        [HttpPost]
        public JsonResult<bool> ChangeIDNO(JObject request)
        {
            string errinfo = "对不起！保存失败，出错原因：";
            try
            {
                var fGuid = request.Value<string>("fGuid");
                var sGuid = request.Value<string>("sGuid");
                if (string.IsNullOrEmpty(fGuid))
                    throw new Utils.PhException("没有找到上传的文件");
                if (string.IsNullOrEmpty(sGuid))
                    throw new Utils.PhException("无效的学校编号");
                var eGuid = StudentLogic.ChangeIDNo(new Guid(fGuid), new Guid(sGuid));
                if (eGuid == Guid.Empty)
                    return new JsonResult<bool>(true);

                errinfo = string.Format("修改身份证失败，请下载结果文件查看详情。<div style='text-align:center; padding:10px'><a href='/Files/DownLoad/{0}' target='_blank'>详情请点击下载Excel文件</a></div>", eGuid);
            }
            catch (Utils.PhException pex) { errinfo = pex.Message; }
            catch (Exception ex)
            {
                errinfo = ex.Message;
                Common.Logger.Error("", ex);
            }
            return new JsonResult<bool>(false, errinfo, false);
        }
        [Route("RemoveStudent")]
        [HttpPost]
        public JsonResult<bool> RemoveStudent(JObject request)
        {
            string errinfo = "对不起！删除失败，出错原因：";
            try
            {
                var cguid = request.Value<string>("classGuid");
                if (!string.IsNullOrEmpty(cguid))
                {
                    var classGuid = new Guid(cguid);
                    StudentLogic.RemoveStudent(classGuid);
                    return new JsonResult<bool>(true);
                }
            }
            catch (Utils.PhException pex) { errinfo = pex.Message; }
            catch (Exception ex)
            {
                errinfo = ex.Message;
                Common.Logger.Error("", ex);
            }
            return new JsonResult<bool>(false, errinfo, false);
        }
    }
}
